//
//  BKDragLoadingView.m
//  XPhoto
//
//  Created by sentinel on 2020/3/26.
//  Copyright © 2020 baboy. All rights reserved.
//

#import "BKDragLoadingView.h"
#import <BKKit/UIImageView+cache.h>
#import <BKKit/UILabel+x.h>
#import <AFNetworking/UIImageView+AFNetworking.h>

#define STATE_LABEL_TEXT_FONT [UIFont systemFontOfSize:14]
#define STATE_LABEL_TEXT_COLOR [UIFont systemFontOfSize:14]

//下拉刷新banner
static NSURL *bkDragLoadingViewRefreshBanner = nil;

//加载更多banner
static NSURL *bkDragLoadingViewLoadMoreBanner = nil;

static CGFloat bkDragLoadingViewVisibleHeight = 60;

@interface BKDragLoadingView()
@property (nonatomic, strong) UILabel *stateLabel;
@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIActivityIndicatorView *indicatorView;
@end

@implementation BKDragLoadingView

- (void)dealloc {
}
- (id)initWithFrame:(CGRect)frame loadingType:(BKDragLoadingViewType) loadingType{
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
        self.loadingType = loadingType;
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
        //设置背景
        CGRect backgroundImageViewFrame = CGRectMake(0, 0, frame.size.width, bkDragLoadingViewVisibleHeight);
        _backgroundImageView = [[UIImageView alloc] initWithFrame:backgroundImageViewFrame];
        _backgroundImageView.contentMode = UIViewContentModeScaleAspectFit;
        
        if (loadingType == BKDragLoadingViewTypeRefresh) {
            if (bkDragLoadingViewRefreshBanner) {
                [_backgroundImageView setImageWithURL:bkDragLoadingViewRefreshBanner placeholderImage:nil];
            }
            backgroundImageViewFrame.origin.y = frame.size.height - backgroundImageViewFrame.size.height;
            _backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
        }
        if (loadingType == BKDragLoadingViewTypeLoadMore) {
            if (bkDragLoadingViewLoadMoreBanner) {
                [_backgroundImageView setImageWithURL:bkDragLoadingViewLoadMoreBanner placeholderImage:nil];
            }
            _backgroundImageView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth;
        }
        _backgroundImageView.frame = backgroundImageViewFrame;
        [self addSubview:_backgroundImageView];
        
        
        //显示区
        CGRect contentFrame = CGRectMake(0, 0, frame.size.width, bkDragLoadingViewVisibleHeight);
        _contentView = [[UIView alloc]  initWithFrame:contentFrame];
        if (loadingType == BKDragLoadingViewTypeRefresh) {
            contentFrame.origin.y = frame.size.height - contentFrame.size.height;
            _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
        }
        if (loadingType == BKDragLoadingViewTypeLoadMore) {
            _contentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin;
        }
        _contentView.frame = contentFrame;
        [_contentView setBackgroundColor:[UIColor blueColor]];
        
        CGFloat textLineHeight = 24;
        CGRect stateLabelFrame = CGRectMake(0, (contentFrame.size.height-textLineHeight)/2, contentFrame.size.width, textLineHeight);
        
        self.stateLabel = createLabel(stateLabelFrame,STATE_LABEL_TEXT_FONT, nil,[UIColor blackColor],[UIColor clearColor],CGSizeMake(0, 0),NSTextAlignmentLeft,0,NSLineBreakByWordWrapping);
        self.stateLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.contentView addSubview:self.stateLabel];
        
        
        
        self.indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        self.indicatorView.frame = CGRectMake( 0, 0, 20.0f, 20.0f );
        self.indicatorView.hidesWhenStopped  = YES;
        [self.contentView addSubview:self.indicatorView];
        [self addSubview:self.contentView];
        
        [self setState:BKDragLoadingViewStateInit];
    }
    return self;
}
- (CGFloat) visibleHeight{
    return bkDragLoadingViewVisibleHeight;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGSize stateLabelSize = [self.stateLabel.text sizeWithAttributes:@{NSFontAttributeName:self.stateLabel.font}];
    CGFloat margin = 10;
    
    CGRect indicatorViewFrame = self.indicatorView.frame;
    indicatorViewFrame.origin.x =  (self.contentView.bounds.size.width - self.indicatorView.bounds.size.width - stateLabelSize.width - margin)/2;
    indicatorViewFrame.origin.y =  (self.contentView.bounds.size.height - self.indicatorView.bounds.size.height)/2;
    
    self.indicatorView.frame = indicatorViewFrame;
    
    CGRect stateLabelFrame = self.stateLabel.frame;
    stateLabelFrame.origin.x = indicatorViewFrame.origin.x + indicatorViewFrame.size.width + margin;
    stateLabelFrame.size.width = stateLabelSize.width + 10;
    self.stateLabel.frame = stateLabelFrame;
    
}
- (void) setState:(BKDragLoadingViewState)state{
    if (state == _state && state != BKDragLoadingViewStateInit)
        return;
    if (_state == BKDragLoadingViewStateLoading && state != BKDragLoadingViewStateLoadFinished) {
        return;
    }
    DLOG(@"dragView state:%ld",state);
    NSString *text = nil;
    switch (state) {
        case BKDragLoadingViewStateInit:
        case BKDragLoadingViewStateLoadFinished:
        case BKDragLoadingViewStateDraging:
            [self.indicatorView stopAnimating];
            text = self.loadingType == BKDragLoadingViewTypeRefresh? NSLocalizedString(@"drag_down_refresh", @"提示"): NSLocalizedString(@"drag_up_load_more", @"提示");
            break;
        case BKDragLoadingViewStateDragBeyond:
            text = self.loadingType == BKDragLoadingViewTypeRefresh? NSLocalizedString(@"release_refresh", @"提示"): NSLocalizedString(@"release_load_more", @"提示");
            break;
        case BKDragLoadingViewStateLoading:
            [self.indicatorView startAnimating];
            text = NSLocalizedString(@"loading", nil);
            break;
        default:
            break;
    }
    self.stateLabel.text = text;
    [self setNeedsLayout];
    _state = state;
}


@end
